package cn.hl.sfzy.service.impl;

import cn.hl.sfzy.entity.Question;
import cn.hl.sfzy.entity.User;
import cn.hl.sfzy.mapper.QuestionMapper;
import cn.hl.sfzy.service.QuestionService;
import cn.hl.sfzy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName QuestionServiceImpl
 * @Description TODO
 * @Author yangshen
 * @Date 2024年10月25日 20:57
 * @Version 1.0
 */

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private UserService userService;
    @Override
    public List<Question> find(Question question) {
        List<Question> questions = questionMapper.find(question);
        for (Question question1 : questions) {
            long userId = question1.getUser_id();
            User user = userService.selectById(userId);
            question1.setUser(user);
        }
        return questions;
    }

    @Override
    public int delete(long id) {
        return questionMapper.delete(id);
    }

    @Override
    public int deleteByUser(long user_id) {
        return questionMapper.deleteByUser(user_id);
    }

    @Override
    public Question selectById(Long id) {
        Question question=questionMapper.selectById(id);
        long userId = question.getUser_id();
        User user = userService.selectById(userId);
        question.setUser(user);
        return question;
    }

    @Override
    public int insert(Question question) {
        return questionMapper.insert(question);
    }
}
